package com.packtpublishing.tddjava.ch03tictactoe;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.net.UnknownHostException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 *
 * Created by admin on 2017/8/10.
 */
public class TicTacToeSpec {
    @Rule
    public ExpectedException exception = ExpectedException.none();
    private TicTacToe ticTacToe;

    @Before
    public final void before() throws UnknownHostException {
        ticTacToe = new TicTacToe();
    }

    @Test
    public void whenXOutsideBoardThenRuntimeException(){
        exception.expect(RuntimeException.class);
        ticTacToe.play(5,2);
    }

    @Test
    public void whenYOutsideBoardThenRuntimeException(){
        exception.expect(RuntimeException.class);
        ticTacToe.play(2,5);
    }

    @Test
    public void whenOccupiedThenRuntimeException(){
        ticTacToe.play(2,1);
        exception.expect(RuntimeException.class);
        ticTacToe.play(2,1);
    }

    @Test
    public void givenFirstTurnWhenNextPlayerThenX(){
        assertEquals('X', ticTacToe.nextPlayer());
    }

    @Test
    public void givenLastTurnWasXWhenNextPlayerThenO(){
        ticTacToe.play(1,1);
        assertEquals('O', ticTacToe.nextPlayer());
    }

    @Test
    public void whenPlayThenNoWinner(){
        String actual = ticTacToe.play(1,1);
        assertEquals("No winner", actual);
    }

    @Test
    public void whenPlayAndWholeHorizontalLineThenWinner(){
        ticTacToe.play(1,1);//X
        ticTacToe.play(1,2);//O
        ticTacToe.play(2,1);//X
        ticTacToe.play(2,2);//O
        String result = ticTacToe.play(3, 1);//X
        assertEquals("X is the winner", result);
    }

    @Test
    public void whenPlayAndWholeVerticalLineThenWinner(){
        ticTacToe.play(1,1);//X
        ticTacToe.play(2,1);//O
        ticTacToe.play(3,1);//X
        ticTacToe.play(2,2);//O
        ticTacToe.play(1,3);//X
        String result = ticTacToe.play(2,3);//O
        assertEquals("O is the winner", result);
    }

    @Test
    public void whenPlayAndBottomTopDiagonalLineThenWinner(){
        ticTacToe.play(1,1);//X
        ticTacToe.play(2,1);//O
        ticTacToe.play(2,2);//X
        ticTacToe.play(2,3);//O
        String result = ticTacToe.play(3,3);//X
        assertEquals("X is the winner", result);
    }

    @Test
    public void whenPlayAndTopBottomDiagonalLineThenWinner(){
        ticTacToe.play(1,3);//X
        ticTacToe.play(2,1);//O
        ticTacToe.play(2,2);//X
        ticTacToe.play(2,3);//O
        String result = ticTacToe.play(3,1);//X
        assertEquals("X is the winner", result);
    }

    @Test
    public void whenAllBoxesAreFilledThenDraw(){
        ticTacToe.play(1,1);//X
        ticTacToe.play(2,2);//O
        ticTacToe.play(2,1);//X
        ticTacToe.play(3,1);//O
        ticTacToe.play(1,3);//X
        ticTacToe.play(1,2);//O
        ticTacToe.play(3,2);//X
        ticTacToe.play(2,3);//O
        String result = ticTacToe.play(3,3);//X
        assertEquals("The result is draw", result);
    }

    @Test
    public void whenInitThenSetCollection(){
        assertNotNull(ticTacToe.getTicTacToeCollection());
    }

}
